﻿using AutoMapper;
using MediatR;
using RedDragon.Common.Domain.AuthorizeRBAC;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.RBAC.ErrorCodeEnum;
using T.Common.Domain.Common;
namespace RedDragon.RBAC.API.Application.Common.Storehouse
{
    public class AddStorehouseHandler : IRequestHandler<AddstorehouseCommand, Result>
    {
        /// <summary>
        /// mapper
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 客户仓储
        /// </summary>
        public readonly IBaseRepository<Storehouses> storehousesRepository;

        public AddStorehouseHandler(IMapper mapper, IBaseRepository<Storehouses> storehousesRepository)
        {
            this.mapper = mapper;
            this.storehousesRepository = storehousesRepository;
        }
        /// <summary>
        /// 入库
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(AddstorehouseCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            Storehouses Storehouses = mapper.Map<Storehouses>(request);
            var res = await storehousesRepository.Add(Storehouses);
            if (res > 0)
            {
                result.Code = (int)CommonErrorCode.Success;
                result.Msg = "入库添加成功";
            }
            return result;
        }
    }
}
